package com.cater.talents.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.BaseEntity;
import com.cater.common.BaseQuery;
import com.cater.common.PageResult;
import com.cater.common.UserContext;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.Dep;
import com.cater.sys.entity.User;
import com.cater.sys.service.DepService;
import com.cater.sys.service.UserService;
import com.cater.talents.entity.UserResume;
import com.cater.talents.service.UserResumeServiceImpl;

/**
 * 描述：简历授权表控制层
 * 
 * @author yong
 * @date 2018/10/14
 */
@Controller
@RequestMapping("/userResume")
public class UserResumeController {

	@Autowired
	private UserResumeServiceImpl service;

	@Autowired
	UserService userService;

	@Autowired
	DepService depService;

	/**
	 * @简历授权表管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "talents/userResume/list";
	}

	/**
	 * @简历授权表管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询简历授权表分页数据")
	public PageResult<UserResume> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<UserResume> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("userResume:add")
	public String addView() {
		return "talents/userResume/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("userResume:add")
	@ResponseBody
	@SystemControllerLog(description = "添加简历授权表")
	public JSONObject add(UserResume t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("userResume:edit")
	public String editView(String id, Model model, CommonQuery query) {
		UserResume t = service.getById(id);
		model.addAttribute("userResume", t);
		return "talents/userResume/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("userResume:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改简历授权表")
	public JSONObject update(UserResume t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("userResume:delete")
	@SystemControllerLog(description = "删除简历授权表")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("userResume:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		UserResume t = service.getById(id);
		model.addAttribute("userResume", t);
		return "talents/userResume/detail";
	}

	@RequestMapping(value = "/openAuth")
	public String openauth(HttpServletRequest request, Model model) {
		String ids = request.getParameter("resumeIds");
		if (StringUtils.isNotEmpty(ids)) {
			model.addAttribute("ids", ids);
		}

		return "talents/userResume/auth_user";
	}

	@ResponseBody
	@RequestMapping(value = "/getUserJson")
	public List<Map<String, Object>> getUserJson(HttpServletRequest request) {
		String ids = request.getParameter("resumeIds");
		String userIds = "";
		if (StringUtils.isNotEmpty(ids)) {
			String[] resumeIds = ids.split(",");
			if (resumeIds.length > 0) {
				List<UserResume> userList = service.getListByResumeId(resumeIds);
				for (UserResume userResume : userList) {
					userIds += userResume.getUseId() + ",";
				}
			}
		}
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;
		List<Dep> deps = depService.getAll();
		List<User> users = userService.getAll();
		Integer dep_id = 0;
		Integer user_id = 0;
		for (Dep t : deps) {
			map = new HashMap<String, Object>();
			dep_id = t.getDepId();
			map.put("id", "d" + dep_id);
			map.put("pId", "d" + t.getCompanyId());
			map.put("name", t.getDepName());
			map.put("nocheck", true);
			map.put("open", true);
			list.add(map);
			for (User user : users) {
				if (dep_id == user.getDep().getDepId()) {
					user_id = user.getId();
					map = new HashMap<String, Object>();
					map.put("id", user_id);
					map.put("pId", "d" + dep_id);
					map.put("name", user.getTruename());
					if (StringUtils.isNotEmpty(userIds)) {
						String[] tid = userIds.split(",");
						for (String id : tid) {
							if (user.getId().equals(Integer.valueOf(id))) {
								map.put("checked", true);
							}
						}

					}
					list.add(map);
				}
			}
		}
		return list;
	}
	/**
	 * 授权简历保存
	 */
	@ResponseBody
	@RequestMapping(value = "/auth")
	@SystemControllerLog(description = "授权简历")
	public JSONObject authsave(@RequestParam String resumeIds, @RequestParam String useIds)
			throws Exception {
		JSONObject json = new JSONObject();
		String authId = String.valueOf(UserContext.getId()); 
		String[] resumeids = resumeIds.split(",");
		String[] useids = useIds.split(",");
		try {
			for (String resumeId : resumeids) {
				for (String userId : useids) {
					Integer num = service.getByResumeIdAndUserId(resumeId, userId);
					if(num==0) {
						UserResume t = new UserResume();
						t.setResumeId(resumeId);
						t.setUseId(userId);
						t.setAuthId(authId);
						service.saves(t);
					}
				}
			}
			json.put("code", 200);
			json.put("message", "授权成功！");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "授权失败！");
		}
		return json;
	}

}